 const app = getApp()
Page({

  /**  
   * 页面的初始数据
   */
  data: {
    activeNames: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    activeNames2: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    receivable:"5",
    img:"",
    list:[],
  },
  onChange(event) {
    this.setData({
      activeName: event.detail
    });
  },
  changeCollapse2(event) {
    this.setData({
      activeNames2: event.detail
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { audit = false, stockOrderId = 0, auxiliaryMaterialsId = 0, smallCanteenPurchaseId = 0, collarId=0, type=0} = JSON.parse(options.data)
  
    this.setData({ audit, stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId, collarId, type})

      if(type===0){
        wx.setNavigationBarTitle({
          title: '新食堂食材进货清单',
        })
        app.reqFetch.getOrderDetails({ stockOrderId }, (res) => {
          const { stockorder, list } = res
          this.setData({ stockorder, list })
        })
      } else if (type === 1){
        wx.setNavigationBarTitle({
          title: '新食堂主食进货清单',
        })
        app.reqFetch.auxiliarymaterialsmxDetails({ auxiliaryMaterialsId }, (res) => {
          const list = res.list
          this.setData({  list })
        })
      } else if (type === 2){
        wx.setNavigationBarTitle({
          title: '小食堂申请清单',
        })
        app.reqFetch.smallcanteenpurchasemxdetails({ smallCanteenPurchaseId }, (res) => {
          const canteensList = JSON.parse(res.smallCanteenPurchase.boxId).list

          this.setData({
            canteensList
          })
          const list = res.list
          this.setData({ list })
        })
      } else if (type === 3) {
        wx.setNavigationBarTitle({
          title: '新食堂主食领取清单',
        })
        app.reqFetch.collarmxdetails({ collarId }, (res) => {
          const { collarMx } = res
          const list = [{ gys: '主食领取列表', mxlist: collarMx }]
          this.setData({ list })
        })
      }
  },
  chooseWxImage: function (type, record) {
    var that = this;
    wx.chooseImage({
      sizeType:  'compressed',
      sourceType: ['camera'],
      success: res=> {
        wx.uploadFile({
          url: 'https://api.canteen.jiangshan.hzzhixun.cn/canteen-api/canteens/oss/upload', 
          filePath: res.tempFilePaths[0],
          name: 'file',
          success:(res)=>{
            const imgUrl =   JSON.parse(res.data).data.src 
            let { list ,type} = that.data
            let { supplierId, foodId, stockOrdermxId, auxiliaryMaterialsmxId, smallCanteenPurchasemxId, collarmxId}=record
            list.map(item => {
              if (item.gysId === supplierId) {
                item.mxlist.map(ele => {
                  if (ele.foodId === foodId) {
                    ele.entityPhoto = imgUrl
                    console.log(ele)
                  }
                })
              }
            })
            that.setData({ list})
          if(type===0){
            app.reqFetch.TakePhoto({
              entityPhoto: imgUrl,
              status: "3",
              stockOrdermxId: stockOrdermxId
            }, (res) => {
            })
          } else if (type === 1){
            app.reqFetch.updatePhoto({
              entityPhoto: imgUrl,
              status: "3",
              auxiliaryMaterialsmxId
            }, (res) => {
            })
          } else if (type === 2){
            app.reqFetch.smallcaupdatePhoto({
              entityPhoto: imgUrl,
              status: "3",
              smallCanteenPurchasemxId
            }, (res) => {
            })
          } else if (type === 3) {
            app.reqFetch.collarmxupdatePhoto({
              entityPhoto: imgUrl,
              status: "3",
              collarmxId
            }, (res) => {
            })
          }
          }
        })

      }
    })
  },
  chooseimage: function (e) {
    if(!this.data.audit){
      console.log(e)
      const { entityPhoto } = e.currentTarget.dataset.record
      this.clickImg(entityPhoto)
      return
    }
    const record = e.currentTarget.dataset.record
    var that = this;
    wx.showActionSheet({
      itemList: [ '拍照'],
      itemColor: "#333333",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album', record)
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera', record)
          }
        }
      }
    })
  },
  clickImg: function (entityPhoto) {

    wx.previewImage({
      urls: [entityPhoto], //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
    changeCollapse(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  arrivalNum: function (e) {
    const { type } = this.data
    const { stockOrdermxId, auxiliaryMaterialsmxId, smallCanteenPurchasemxId, collarmxId} = e.target.dataset.record
    
    if (type===0){
      app.reqFetch.TakePhoto({
        arrivalNum: e.detail.value,
        status: "3",
        stockOrdermxId: stockOrdermxId
      }, (res) => {
      })
    } else if (type === 1){
      app.reqFetch.updatePhoto({
        arrivalNum: e.detail.value,
        status: "3",
        auxiliaryMaterialsmxId
      }, (res) => {
      })
    } else if (type === 2){
      app.reqFetch.smallcaupdatePhoto({
        arrivalNum: e.detail.value,
        status: "3",
        smallCanteenPurchasemxId
      }, (res) => {
      })
    } else if (type === 3) {
      app.reqFetch.collarmxupdatePhoto({
        realCollarNum: e.detail.value,
        status: "3",
        collarmxId
      }, (res) => {
      })
    }



  },

  Submission: function (e) {
    const { stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId,collarId,type } = this.data
    const canteenAdminId = app.globalData.userId
    if(type===0){
      app.reqFetch.pageAdminUpdate({ stockOrderId, status: 3, canteenAdminId }, (res) => {
        console.log(res)
        if (res.code === 0) {
          wx.showToast({
            title: '新食堂食材进货清单验收成功',
            icon: 'none'
          })
          setTimeout(function () {
          
            wx.navigateBack({
              delta: 1,
            })
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    } else if (type === 1){
      app.reqFetch.pageAdminUpdate2({ auxiliaryMaterialsId, status: 3, canteenAdminId }, (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '新食堂主食进货清单验收成功',
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }

      })
    } else if (type === 2){
      app.reqFetch.smallcanteenpurchasepageAdminUpdate({ smallCanteenPurchaseId, status: 3, canteenAdminId }, (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '小食堂申请清单验收成功',
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    } else if (type === 3) {
      app.reqFetch.collarpageAdminUpdate({ collarId, status: 3, canteenAdminId}, (res) => {
        if (res.code === 0) {
          wx.showToast({
            title: '新食堂主食领取清单验收成功',
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 700)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    }
  },
  handleCancel(){
    wx.navigateBack({
      delta: 1,
    })
  }
})